layui.use('layer', function(){
    var $ = layui.jquery;
    var layer = layui.layer;

    var verifyCode = new GVerify("v_container");



    $("#logingBut").click(function () {
        onsubmit();
    })
    function onsubmit(){
        var oldPassword = $("#oldPassword").val();
        var newPassword = $("#newPassword").val();
        var repetitionPassword = $("#repetitionPassword").val();
        var verification = $("#verification").val();
        var res = verifyCode.validate(verification);
        if(oldPassword == ""){
            top.layer.msg("原密码不能为空!", {icon: 2});
        }else if (newPassword == ""){
            top.layer.msg("新密码不能为空!", {icon: 2});
        }else if(repetitionPassword == ""){
            top.layer.msg("重复密码不能为空!", {icon: 2});
        }else if(newPassword != repetitionPassword){
            top.layer.msg("两次密码输入不一致!", {icon: 2});
        }else if(verification == "") {
            top.layer.msg("验证码不能为空!", {icon: 2});
        }else if(!res){
            top.layer.msg("验证码输入错误，请重新输入!", {icon: 2});
            $("#verification").val("");
        }else{
            $.ajax({
                url: "/user/login?userName="+$("#userName").val()+"&userPassword="+oldPassword,
                dataType: "json",
                type: "post",
                contentType: "application/json;",
                success: function (data) {
                    if(data.code == 0){
                        $.ajax({
                            url: "/changePassword/updatePassword?userName="+$("#userName").val()+"&userPassword="+newPassword,
                            dataType: "json",
                            type: "post",
                            contentType: "application/json;",
                            success: function (data) {
                                if(data.code == 0){
                                    top.layer.msg("修改成功!", {icon: 1});
                                }else{
                                    top.layer.msg(data.msg , {icon: 2});
                                }
                            }
                        });
                    }else{
                        top.layer.msg("原密码不正确" , {icon: 2});
                    }
                }
            });
        }
    }

})
